package com.itheima.leetcode.od.c.queue;

import java.util.*;
import java.util.stream.Collectors;

/**
 * <h3>约瑟夫问题</h3>
 */
public class JosephusProblem {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);

        Integer[] input_arr =
                Arrays.stream(sc.nextLine().split(",")).map(Integer::parseInt).toArray(Integer[]::new);

        int len = Integer.parseInt(sc.nextLine());

        int m = Integer.parseInt(sc.nextLine());

        System.out.println(getResult(input_arr, len, m));
    }

    public static String getResult(Integer[] input_arr, int len, int m) {
        LinkedList<Integer> dq = new LinkedList<>(Arrays.asList(input_arr));

        ArrayList<Integer> output_arr = new ArrayList<>();

        int i = 1;
        while (len > 0) {
            Integer out = dq.removeFirst();

            if (i == m) {
                output_arr.add(out);
                m = out;
                i = 1;
                len--;
            } else {
                dq.add(out);
                i++;
            }
        }

        return output_arr.stream()
                .map(String::valueOf)
                .collect(Collectors.joining(","));
    }
}